package dao

import (
	"context"
	"mini-upload/internal/model/entity"

	"github.com/marcellowy/go-common/gogf/vlog"
	"github.com/marcellowy/go-common/tools"
	"github.com/marcellowy/go-common/vid/snowflake"
	"gorm.io/gorm"
)

type Attachment struct {
}

func NewAttachment() *Attachment {
	return &Attachment{}
}

func (a *Attachment) Create(ctx context.Context, data *entity.Attachment) (err error) {
	return a.create(ctx, db, data)
}

func (a *Attachment) create(ctx context.Context, tx *gorm.DB, data *entity.Attachment) (err error) {
	if data.AttachmentId, err = snowflake.NextID(); err != nil {
		vlog.Warning(ctx, err)
		return
	}
	if err = tx.Create(data).Error; err != nil {
		vlog.Warning(ctx, err, tools.JSONMarshalString(data))
	}
	return
}
